<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
          <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="意向等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择意向等级" clearable>
          <el-option v-for="dict in level" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery" style="margin-right: 20px;">重置</el-button>
        <RouterLink to="/follow"  class="custom-button">联络簿</RouterLink>
      </el-form-item>
    </el-form>

    <el-divider />

    <el-table v-loading="loading" :data="screeningList" @selection-change="handleSelectionChange">
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="用户姓名" align="center" prop="nickName" />
      <el-table-column label="用户邮箱" align="center" prop="email" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="用户性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="意向等级" align="center" prop="level">

        <template #default="scope">
          <dict-tag :options="level" :value="scope.row.level" />
        </template>
      </el-table-column>
      <el-table-column label="空闲状态" align="center" prop="idleState">

        <template #default="scope">
          <dict-tag :options="idle_state" :value="scope.row.idleState" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:screening:edit']">修改</el-button>
          <el-button link type="primary" icon="plus" @click="addFollow(scope.row.userId)"
            v-hasPermi="['manage:followers:add']">添加到联络簿</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改招聘人员对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="screeningRef" :model="form" :rules="rules" label-width="120px">
        <el-col :span="12">
          <el-form-item label="是否邀请面试">
            <el-select v-model="form.idleState" placeholder="请选择">
              <el-option v-for="dict in status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Screening">
import { listScreening, getScreening, delScreening, addScreening, updateScreening } from "@/api/manage/screening";
import { listFollowers, getFollowers, delFollowers, addFollowers, updateFollowers } from "@/api/manage/followers";

const { proxy } = getCurrentInstance();
const { sys_user_sex, idle_state, level } = proxy.useDict('sys_user_sex', 'idle_state', 'level');

// 修改求职人闲置状态
const status = [
  { label: "邀请面试", value: 2 },
  { label: "退回公海", value: 1 }
]
const screeningList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sex: null,
    level: null,
    idleState: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询招聘人员列表 */
function getList() {
  loading.value = true;
  listScreening(queryParams.value).then(response => {
    screeningList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 添加人员到联络簿 */
function addFollow(id) {
  console.log("id:"+id);
  addFollowers(id).then(response => {
    proxy.$modal.msgSuccess("添加成功");
    getList();
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    deptId: null,
    userName: null,
    nickName: null,
    userType: null,
    email: null,
    phonenumber: null,
    sex: null,
    avatar: null,
    password: null,
    status: null,
    delFlag: null,
    loginIp: null,
    loginDate: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    level: null,
    idleState: null
  };
  proxy.resetForm("screeningRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = BigInt(row.userId) || ids.value
  getScreening(_userId).then(response => {
    form.value = response.data;
    console.log("_userId:" + _userId);
    open.value = true;
    title.value = "修改招聘人员";
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["screeningRef"].validate(valid => {
    if (valid) {
      console.log("form.value.userId:" + form.value.userId);
      if (form.value.userId != null) {
        updateScreening(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

getList();
</script>

<style>
.custom-button {
  display: inline-block;
  padding: 12px 20px; /* 调整按钮的内边距 */
  background-color: #409EFF; /* 设置背景色 */
  color: #fff; /* 设置文字颜色 */
  border-radius: 4px; /* 设置圆角 */
  text-decoration: none; /* 去掉下划线 */
  cursor: pointer; /* 鼠标悬停时变为手型 */
  transition: background-color 0.3s; /* 添加过渡效果 */
}

.custom-button:hover {
  background-color: #66b1ff; /* 鼠标悬停时改变背景色 */
}
</style>